package org.jackysoft.controller;

import java.util.Collection;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jackysoft.annotations.Logger;
import org.jackysoft.controller.base.AbstractController;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.User;
import org.jackysoft.service.DepartmentService;
import org.jackysoft.util.OperationType;
import org.jackysoft.ztree.DepartmentZtreeFunction;
import org.jackysoft.ztree.ZTreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Collections2;

@Logger(label = "部门", logType = OperationType.System)
@Controller
@RequestMapping("/department")
public class DepartmentController extends AbstractController<Department> {
	
	@Resource
	private DepartmentService service;
	
	
	@RequestMapping("/room")
	public void room(@ModelAttribute Collection<Department> beans) throws Exception {
		Subject u = SecurityUtils.getSubject();
		String did = ((User) u.getPrincipal()).getDepartment().getId();
		beans = service.getChildren(did);
	
	}

	@RequestMapping("/byroot")
	public void byroot(@ModelAttribute Collection<Department> beans) throws Exception {
		beans = service.getChildren("");
		
	}

	@RequestMapping("/bytype")
	public void bytype(int dtype ,@ModelAttribute Collection<Department> beans) throws Exception {
		beans = service.getChildren(dtype);	
	}
	
	
	@RequestMapping("/roomonly")
	@ResponseBody
	public Collection<ZTreeNode> roomonly() {
		Collection<ZTreeNode> beans = Collections2.transform(service.getChildren(1),new DepartmentZtreeFunction());
		return beans;
	}
	
}
